<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../../Css/style.css" />
    <script type="text/javascript" src="../../Js/jquery.js"></script>
    <script type="text/javascript" src="../../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../../Js/ckform.js"></script>
    <script type="text/javascript" src="../../Js/common.js"></script>

 

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
</head>
<body>
<form action="" method="post" class="definewidth m20">
<input type="hidden" name="id" value="{$user.id}" />
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">登录名</td>
            <td id="uLoginname">-</td>
        </tr>
        <tr>
            <td class="tableleft">密码</td>
            <td><input id="uPassword" type="text" name="password"/><span id="passMsg"></span></td>
        </tr>
        <tr>
            <td class="tableleft">真实姓名</td>
            <td><input id="uTruename" type="text" name="realname"/><span id="truenameMsg"></span></td>
        </tr>
        <tr>
            <td class="tableleft">邮箱</td>
            <td><input id="uEmail" type="text" name="email"/><span id="emailMsg"></span></td>
        </tr>
        <tr>
            <td class="tableleft">状态</td>
            <td>
                <input type="radio" name="status" value="0"/> 启用
              <input type="radio" name="status" value="1" /> 禁用
            </td>
        </tr>
        <tr>
            <td class="tableleft">角色</td>
            <td>
            	<select name="role" id="role">

       			 </select>
        	</td>
        </tr>
        <tr>
            <td class="tableleft"></td>
            <td>
                <button id="update" type="button" class="btn btn-primary" type="button">更新</button>&nbsp;&nbsp;<button type="button" class="btn btn-success" name="backid" onclick="back()" id="backid">返回列表</button>
            </td>
        </tr>
    </table>
</form>
<script>
    function validataPwd() {
        var pwd = $("#uPassword").val();
        var msg = $("#passMsg");
        if(pwd == "") {
            msg.html("密码不能为空").css("color", "red");
            return false;
        } else if(pwd.length < 8) {
            msg.html("密码长度必须大于8位").css("color", "red");
            return false;
        } else {
            msg.html("ok").css("color", "green");
            return true;
        }
    }

    function validataTruename() {
        var tname = $("#uTruename").val();
        var msg = $("#truenameMsg");
        if(tname == "") {
            msg.html("真实姓名不能为空").css("color", "red");
            return false;
        } else {
            msg.html("ok").css("color", "green");
            return true;
        }
    }


    function validataEmail() {
        var email = $("#uEmail").val();
        var msg = $("#emailMsg");
        var emailtest=/^\w{5,}@[a-z0-9]{2,3}\.[a-z]+$|\,$/;
        if(email == "") {
            msg.html("邮箱不能为空").css("color", "red");
            return false;
        } else if (!emailtest.test(email)){
            msg.html("邮箱格式不正确").css("color", "red");
            return false;
        }
        else {
            msg.html("ok").css("color", "green");
            return true;
        }
    }

    function validataState() {
        var all = $("input[name='status']");
        for(var i = 0; i < all.length; i++) {
            if($(all[i]).prop("checked")) {
                return true;
            }
        }
        alert("状态为必选项");
        return false;
    }

    function validataRole() {
        var ca = $("#role").val();
        if(ca == 0) {
            alert("角色为必选项");
            return false;
        } else {
            return true;
        }
    }

    $(function() {
        $("#uPassword").blur(function() {
            validataPwd();
        });

        $("#uTruename").blur(function() {
            validataTruename();
        });

        $("#uEmail").blur(function() {
            validataEmail();
        });
    });
    String.prototype.GetValue= function(para) {
        let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
        let r = this.substr(this.indexOf("\?")+1).match(reg);
        if (r!=null) return unescape(r[2]); return null;
    }
    let uId=null;

    function back(){
        window.location.href="/pages/user/index.html";
    }

    $(function () {

        let url=window.location.toString();
        uId=url.GetValue("uId");
        $.getJSON("/user/edit.do",{uId:uId},function (vo){
            let list=vo.list;
            let obj=vo.object;
            let loginName=obj.uLoginname;
            let password=obj.uPassword;
            let trueName=obj.uTruename;
            let email=obj.uEmail;
            let state=obj.uState;
            let rId=obj.rId;
            let str="<option value=\"\">--"+"请选择"+"--\n";
            for (let i=0;i<list.length;i++){
                if (rId!=list[i].rId){
                    str+="<option value=\""+list[i].rId+"\">"+list[i].rName+"\n";
                }else {
                    str+="<option value=\""+list[i].rId+"\" selected>"+list[i].rName+"\n";
                }

            }
            $("#uLoginname").html(loginName);
            $("#uPassword").val(password);
            $("#uTruename").val(trueName);
            $("#uEmail").val(email);
            if (state==0){
                $("input[name='status']:eq(0)").prop("checked",true);
            }else if (state==1){
                $("input[name='status']:eq(1)").prop("checked",true);
            }
            $("#role").html(str);
        });

    });
    $("#update").click(function (){
        if (validataPwd() && validataTruename() && validataEmail() &&validataState() &&validataRole()){
            if (confirm("是否更新?")){
                let userId=uId;
                let loginName=$("#uLoginName").html();
                let password=$("#uPassword").val();
                let trueName=$("#uTrueName").val();
                let email=$("#uEmail").val();
                let state=$("input[name='status']:checked").val();
                let rId=$("#role option:selected").val();
                let user={uId:userId,uLoginname:loginName,uPassword:password,uTruename:trueName,uEmail:email,uState:state,rId:rId[0]};
                $.getJSON("/user/update.do",user,function (vo){
                    if(vo.code==200) {
                        window.location.href = "/pages/user/index.html";
                    }else{
                        alert("更新失败！"+vo.msg);
                    }
                });
            }
        }
    });
</script>
</body>
</html>
